// The MIT License
// 
// Copyright (c) 2008 Werner Strydom
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "MetaWeblog.h"

@implementation MetaWeblog

@synthesize url; 
@synthesize username;
@synthesize password;
@synthesize blogId; 

- (id) init
{
	url = nil; 
	username  = nil;
	password = nil;
	blogId = nil;
	
	[super init]; 
	return self;
}

- (NSString*)newPost: (Post*) px
{
	
	NSString* title = px.title;
	NSString* summary = px.summary;
	NSString* body = px.description;
	NSDate* date = px.dateCreated;
	NSString* more = px.more;
	NSArray* categories = [[NSArray alloc] initWithArray: px.categories copyItems: YES];
	
	NSString *description = body;
	WSMethodInvocationRef rpcCall;
	NSURL *rpcURL = [NSURL URLWithString: url];
	NSString *methodName = @"metaWeblog.newPost";
	NSArray *postKey = [NSArray arrayWithObjects: 
							@"description", 
							@"dateCreated", 
							@"title", 
							@"mt_excerpt", 
							@"mt_text_more", 
							@"categories", 
							nil];
	NSArray *postObject = [NSArray arrayWithObjects:
						   description, 
						   date, 
						   title, 
						   summary, 
						   more, 
						   categories, 
						   nil];
	NSDictionary *post = [NSDictionary dictionaryWithObjects: postObject forKeys:postKey]; 
	NSArray *keys = [NSArray arrayWithObjects: @"blog_ID" , @"username", @"password", @"post", @"publish", nil];
	NSMutableArray *objects = [NSArray arrayWithObjects: blogId, username, password, post, kCFBooleanTrue, nil];
	NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
	NSArray *order = [NSArray arrayWithObjects: @"blog_ID", @"username", @"password", @"post", @"publish", nil];
	rpcCall = WSMethodInvocationCreate ((CFURLRef) rpcURL, (CFStringRef) methodName, kWSXMLRPCProtocol);
	WSMethodInvocationSetParameters (rpcCall, (CFDictionaryRef) params, (CFArrayRef) order);
	NSDictionary *result = (NSDictionary *) (WSMethodInvocationInvoke (rpcCall));
	if (WSMethodResultIsFault((CFDictionaryRef)result))
	{
		NSString* faultString = [result objectForKey: (NSString *) kWSFaultString];
		NSLog(@"[metaWeblog.newPost] failed = %@", result);	
		NSException* theException = [NSException exceptionWithName: @"MetaWeblogException" reason: faultString userInfo:nil]; 
		@throw theException; 
	}
	
	NSLog(@"[metaWeblog.newPost] result = %@", result);	
	return (NSString*) [result objectForKey:(NSString*)kWSMethodInvocationResult ];
}

- (NSString*)newMediaObject: (NSString*)name andType: (NSString*) type andBits:(NSData*) bits
{
	
	WSMethodInvocationRef rpcCall;
	NSURL *rpcURL = [NSURL URLWithString: url];
	NSString *methodName = @"metaWeblog.newMediaObject";
	NSArray *postKey = [NSArray arrayWithObjects: @"name", @"type", @"bits", nil];
	NSArray *postObject = [NSArray arrayWithObjects: name, type, bits, nil];
	NSDictionary *post = [NSDictionary dictionaryWithObjects:postObject forKeys:postKey]; 
	NSArray *keys = [NSArray arrayWithObjects: @"blog_ID" , @"username", @"password", @"post", nil];
	NSMutableArray *objects = [NSArray arrayWithObjects: blogId, username, password, post, nil];
	NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
	NSArray *order = [NSArray arrayWithObjects: @"blog_ID", @"username", @"password", @"post", nil];
	rpcCall = WSMethodInvocationCreate ((CFURLRef) rpcURL, (CFStringRef) methodName, kWSXMLRPCProtocol);
	WSMethodInvocationSetParameters (rpcCall, (CFDictionaryRef) params, (CFArrayRef) order);
	
	NSDictionary *result = (NSDictionary *) (WSMethodInvocationInvoke (rpcCall));
	NSLog(@"[metaWeblog.newMediaObject] result = %@", result);
	
	NSDictionary *values = [result objectForKey:(NSString*)kWSMethodInvocationResult ];
	return (NSString*) [values objectForKey: @"url"];
}


@end

